home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / other.c < prev    next >
C/C++ Source or Header  |  1992-08-12  |  10KB  |  382 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* other.c */
  25.  
  26. #include "builtin.h"
  27. #include  <errno.h>
  28. #include <ctype.h>
  29. #include  <sys/types.h>
  30. #include <sys/syscall.h>
  31.  
  32. extern float    floatval();
  33. extern LONG_PTR insert();
  34. extern WORD     d_trace, d_hitrace;
  35. /* extern WORD     errno; */
  36. extern LONG     maxmem, maxpspace, maxtrail;
  37. extern LONG    simpath;
  38.  
  39. static BYTE perm = PERM;
  40.  
  41. FILE   *curr_in, *curr_out;   /* current input, output streams */
  42. /*
  43. typedef union {
  44.        CHAR_PTR name;
  45.        LONG     num;
  46. } call_args;
  47. */
  48. typedef unsigned int call_args;
  49.  
  50. static call_args call_arg[10];
  51. static CHAR      s[256];
  52.  
  53. b_GET_SIMPATH() /* X */
  54. {
  55.     if (!unify(reg[1], simpath)) {FAIL0;}
  56. }
  57.  
  58. b_SYSTEM0()  /* reg1: a list of int (string) for CShell commands */
  59. {
  60.    register LONG     op1;
  61.    register LONG_PTR top;
  62.    CHAR     s[256];
  63.  
  64.    op1 = reg[1];  DEREF(op1);
  65.    namestring(GET_STR_PSC(op1), s);
  66.    if (!unify(MAKEINT(system(s)), reg[2]))
  67.       {FAIL0;}
  68. }
  69.  
  70.  
  71. getgenargs(rno)
  72. WORD rno;
  73. {  /* rno is number of register containing list of args
  74.     * This routine converts them into array cal_arg, and
  75.     * returns the number of args
  76.     */
  77.    register LONG     op2, op3;
  78.    register LONG_PTR top;
  79.    PSC_REC_PTR       psc_ptr;
  80.    WORD              i;
  81.  
  82.    op2 = reg[rno];  DEREF(op2);
  83.    i = 1;
  84.    while (!ISNIL(op2)) {
  85.       UNTAG(op2);
  86.       op3 = FOLLOW(op2);  DEREF(op3);
  87.       if (ISATOM(op3)) {
  88.          psc_ptr = GET_STR_PSC(op3);
  89.          if (IS_ORDI(psc_ptr)) {
  90.             namestring(psc_ptr, s);
  91.             call_arg[i] = (call_args) s;
  92.          } else if (IS_BUFF(psc_ptr))
  93.             call_arg[i] = (call_args) GET_NAME(psc_ptr);
  94.       }
  95.       else if (ISINTEGER(op3)) call_arg[i] = INTVAL(op3);
  96.       else
  97.          quit("Unknown syscall argument\n");
  98.       op2 += 4;
  99.       DEREF(op2);
  100.       i++;
  101.    }
  102.    return i;
  103. }
  104.  
  105. b_SYSCALL()  /* r1: call # ; R2: a list of parameters; R3: returned value */
  106. {
  107.     register LONG      op1;
  108.     register LONG_PTR  top;
  109.     int                n, r;
  110.  
  111.     op1 = reg[1];  DEREF(op1);
  112.     n   = INTVAL(op1);              /* syscall number */
  113.     switch ( getgenargs(2) ) {
  114.       case  1: r = syscall(n);
  115.            break;
  116.       case  2: r = syscall(n, call_arg[1]);
  117.            break;
  118.       case  3: r = syscall(n, call_arg[1], call_arg[2]);
  119.            break;
  120.       case  4: r = syscall(n, call_arg[1], call_arg[2], call_arg[3]);
  121.            break;
  122.       case  5: r = syscall(n, call_arg[1], call_arg[2], call_arg[3],
  123.                   call_arg[4]);
  124.            break;
  125.       case  6: r = syscall(n, call_arg[1], call_arg[2], call_arg[3],
  126.                   call_arg[4], call_arg[5]);
  127.            break;
  128.       case  7: r = syscall(n, call_arg[1], call_arg[2], call_arg[3],
  129.                   call_arg[4], call_arg[5], call_arg[6]);
  130.            break;
  131.       default: quit( "Too many arguments for syscall\n" );
  132.            break;
  133.    }
  134.    if ( ! unify( reg[3], MAKEINT(r)) ) {
  135.       FAIL0;
  136.    }
  137. }
  138.  
  139. b_ERRNO()
  140. {
  141.    if (!unify( reg[1], MAKEINT(errno)))
  142.       {FAIL0;}
  143. }
  144.  
  145.  
  146. b_CALL()  /* reg1: The predicate to be called */
  147. {
  148.    callv_sub();  /* since cpreg has been saved by call "call",
  149.                     should not be saved again, the same as exec */
  150. }
  151.  
  152.  
  153. b_LOAD()
  154. {  /* reg1: the byte code file to be loaded
  155.     * reg2: the return code, 0 => success
  156.     */
  157.    register LONG     op1;
  158.    register LONG_PTR top;
  159.    PSC_REC_PTR         psc_ptr;
  160.  
  161.    op1 = reg[1];  DEREF(op1);
  162.    psc_ptr = GET_STR_PSC(op1);
  163.            
  164.    if (!unify(MAKEINT(dyn_loader(psc_ptr)), reg[2]))
  165.       {FAIL0;}
  166. }
  167.  
  168.  
  169. b_STATISTICS()
  170. {
  171.    print_statistics();
  172. }
  173.  
  174.  
  175. b_STATISTICS0()
  176. {
  177.    if (!unify(reg[1], MAKEINT(maxpspace*sizeof(LONG))))
  178.       {FAIL0;}     /* max program area */
  179.    if (!unify(reg[2], MAKEINT((((LONG)curr_fence-(LONG)pspace)))))
  180.       {FAIL0;}     /* program area in use */
  181.    if (!unify(reg[3], MAKEINT((hreg-heap_bottom))))
  182.       {FAIL0;}     /* global stack in use */
  183.    if (!unify(reg[4], MAKEINT((ereg<breg?ereg:breg)-hreg)))
  184.       {FAIL0;}     /* stack area (local, global) free */
  185.    if (!unify(reg[5], MAKEINT(local_bottom-(ereg<breg?ereg:breg))))
  186.       {FAIL0;}     /* local stack in use */
  187.    if (!unify(reg[6], MAKEINT(maxmem*sizeof(LONG))))
  188.       {FAIL0;}     /* total stack area size */
  189.    if (!unify(reg[7], MAKEINT(maxtrail*sizeof(LONG))))
  190.       {FAIL0;}     /* total trail size */
  191.    if (!unify(reg[8], MAKEINT(trreg-tstack)))
  192.       {FAIL0;}
  193. }
  194.  
  195.  
  196. b_TRACE()
  197. {
  198.    hitrace = 1;
  199. }
  200.  
  201.  
  202. b_PILTRACE()
  203. {
  204.    trace = 1;
  205. }
  206.  
  207.  
  208. b_UNTRACE()
  209. {
  210.    hitrace = trace = 0;
  211. }
  212.  
  213. /*
  214. b_DETRACE()
  215. {
  216.    hitrace = d_hitrace;
  217.    trace = d_trace;
  218. }
  219. */
  220.  
  221. b_SYMTYPE()  /* reg1 term, reg2 type field of psc-entry of root sym of term */
  222. {
  223.    register LONG     op1;
  224.    register LONG_PTR top;
  225.  
  226.    op1 = reg[1];
  227. typd:
  228.    switch (TAG(op1)) {
  229.       case FREE: NDEREF(op1, typd);
  230.       case LIST:
  231.       case NUM : quit("Symtype: illegal first arg\n");
  232.       case CS  : if (!unify(MAKEINT(GET_ETYPE(GET_STR_PSC(op1))), reg[2]))
  233.                     {FAIL0;}
  234.    }
  235. }
  236.  
  237.  
  238. b_HASHVAL()  /* reg1 Arg, reg2 size of hashtab, reg3 hashval for this arg */
  239. {
  240.    register LONG     op1, op2, op3;
  241.    register LONG_PTR top;
  242.  
  243.    op1 = reg[1];
  244.    op2 = reg[2];  DEREF(op2);  op2 = INTVAL(op2);
  245.    op3 = reg[3];  DEREF(op3);
  246. sotd0:
  247.    switch(TAG(op1)) {
  248.       case FREE: NDEREF(op1, sotd0);
  249.                  printf("Indexing for asserted predicate with var arg\n");
  250.                  FAIL0;
  251.       case NUM : if (ISINTEGER(op1))
  252.                     op1 = INTVAL(op1);
  253.                  else
  254.                     op1 = (LONG)(floatval(op1));
  255.                  break;
  256.       case LIST: op1 = *((LONG_PTR)UNTAGGED(list_str));
  257.                  break;
  258.       case CS  : op1 = (LONG)GET_STR_PSC(op1);
  259.                  break;
  260.    }
  261.    if (!unify(op3, MAKEINT(IHASH(op1, op2))))
  262.       {FAIL0;}
  263. }
  264.  
  265.  
  266. b_FLAGS()
  267. {  /* reg1 contains number of bit to get or set (must be integer);
  268.     * reg2 contains setting of 0 or 1,
  269.     * or is variable and setting will be returned
  270.     */
  271.    register LONG     op1, op2, res;
  272.    register LONG_PTR top;
  273.  
  274.    op1 = reg[1];  DEREF(op1);  op1 = INTVAL(op1);
  275.    op2 = reg[2];  DEREF(op2);
  276.    if (ISNONVAR(op2)) {
  277.       if (op1 > 9)
  278.          flags[op1-10] = op2;
  279.       else {
  280.          op2 = INTVAL(op2);
  281.          switch (op1) {
  282.             case 0: trace      = op2;  break;
  283.             case 1: hitrace    = op2;  break;
  284.             case 2: overflow_f = op2;  break;
  285.             case 3: trace_sta  = op2;  break;
  286.          }
  287.          call_intercept = hitrace | trace_sta;
  288.       }
  289.    } else {
  290.       if (op1 > 9)
  291.          res = flags[op1-10];
  292.       else {
  293.          switch (op1) {
  294.             case 0: res = trace;       break;
  295.             case 1: res = hitrace;     break;
  296.             case 2: res = overflow_f;  break;
  297.             case 3: res = trace_sta;   break;
  298.          }
  299.          res = MAKEINT(res);
  300.       }
  301.       FOLLOW(op2) = res;
  302.    }
  303. }
  304.  
  305.  
  306. print_statistics()
  307. {
  308.     LONG_PTR lstktop;
  309.  
  310.     if (breg < ereg)
  311.        lstktop = breg;
  312.     else
  313.        lstktop = ereg - *(cpreg-5);
  314.  
  315.     printf("Maximum available stack size: %d\n", maxmem);
  316.     printf("  Local stack: %d in use, %d max used.\n",
  317.            local_bottom-lstktop, local_bottom-mlocaltop);
  318.     printf("  Heap stack: %d in use, %d max used.\n",
  319.            hreg-heap_bottom, mheaptop-heap_bottom);
  320.  
  321.     printf("Permanent space: %d, %d in use.\n", maxpspace,
  322.            ((int) curr_fence - (int) pspace)/4);
  323.  
  324.     printf("Trail stack: %d, %d in use, %d max used.\n",
  325.            maxtrail, trail_bottom-trreg, trail_bottom-mtrailtop);
  326. }
  327.  
  328.  
  329. b_READNAME()
  330. {  /* Ch, Name, NextCh: reads a sequence of letters, digits
  331.     * dollar signs and underscores, makes the sequence into a
  332.     * constant and inserts it into the PSC table if necessary,
  333.     * and returns a pointer to the PSC entry as Name.  NextCh is
  334.     * the first character read which cannot join this sequence.
  335.     *
  336.     * It is assumed that reg1 contains a character; and that regs
  337.     * 2 and 3 are free.  No checking is done here.  This builtin
  338.     * is only supposed to be called from $read_tokens/3 anyway.
  339.     */
  340.  
  341.    register LONG     op;
  342.    register LONG_PTR top;
  343.    CHAR     pname[STR_LIM], ch;
  344.    CHAR_PTR name;
  345.    WORD     len = 1;
  346.    WORD     done = 0;
  347.    LONG     ptr;
  348.  
  349.    op = reg[1];  DEREF(op);  op = INTVAL(op);
  350.  
  351.    name = pname;
  352.    *name++ = (CHAR)op;   /* first character in the sequence */
  353.  
  354.    while (!done && len <= STR_LIM) {
  355.       ch = getc(curr_in);
  356.       if (isalpha(ch) || isdigit(ch) || ch == '$' || ch ==  '_') {
  357.          *name++ = ch;
  358.          len++;
  359.       } else {
  360.          *name = '\0';
  361.          done = 1;
  362.       }
  363.    }
  364.  
  365.    if (ch == EOF) {
  366.       clearerr(curr_in);
  367.       printf("! unexpected end of file after %s\n", pname);
  368.    }
  369.    if (len > STR_LIM) {
  370.       *name = '\0';
  371.       len--;
  372.       printf("*** Name of constant too long: %s\n", pname);
  373.    }
  374.  
  375.    ptr = (LONG)insert(pname, len, 0, &perm) | CS_TAG;
  376.    if (!unify(reg[2], ptr))
  377.        {FAIL0;}
  378.    op = reg[3];  DEREF(op);
  379.    FOLLOW(op) = MAKEINT(ch);
  380.    PUSHTRAIL(op);
  381. }
  382.